package myclient;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 * @author JIAO
 */
public class SearchFrame {
	boolean bFind = true;//true 为精确查找,false为条件查找
	PrintWriter writer = null;
	private JFrame searchFrame;
	private JPanel search_pn;
	private JPanel result_pn;
	private JList result_pn_list;
	private JButton bt_search;
	private JButton bt_previous;
	private JButton bt_addFriend;
	
	//精确查询的初始条件
	private String age = "年龄不限";
	private String sex = "性别不限";
	private String city = "中国";
	private String isOnline = "所有用户";
	
	public SearchFrame(final PrintWriter writer,final MainFrame mainFm){
		this.writer = writer;
		searchFrame = new JFrame();
		searchFrame.setResizable(false);
		searchFrame.setBounds(440, 330, 400, 350);
		searchFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JPanel searchStyle_pn = new JPanel();
		JRadioButton searchStyle_bt_exact = new JRadioButton("精确查找");
		JRadioButton searchStyle_bt_condition = new JRadioButton("条件查找");
		
		
		ButtonGroup group = new ButtonGroup();
		group.add(searchStyle_bt_exact);
		group.add(searchStyle_bt_condition);
		searchStyle_pn.setLayout(new GridLayout(2,1));
		searchStyle_pn.add(searchStyle_bt_exact);
		searchStyle_pn.add(searchStyle_bt_condition);
		
		
		//精确查找panel
		final JPanel exactSearch_pn = new JPanel();
		JLabel exactSearch_lb_Id = new JLabel("账号：");
		JLabel exactSearch_lb_Name = new JLabel("昵称：");
		final JTextField exactSearch_text_Id = new JTextField();
		final JTextField exactSearch_text_Name = new JTextField();
		exactSearch_pn.setLayout(new GridLayout(7,1));
		exactSearch_pn.add(new JLabel());
		exactSearch_pn.add(exactSearch_lb_Id);
		exactSearch_pn.add(exactSearch_text_Id);
		exactSearch_pn.add(exactSearch_lb_Name);
		exactSearch_pn.add(exactSearch_text_Name);
		
		
		search_pn = new JPanel();
		search_pn.setBorder(new TitledBorder("查找方式"));
		search_pn.setLayout(new BorderLayout());
		search_pn.add(searchStyle_pn,BorderLayout.NORTH);
		search_pn.add(exactSearch_pn,BorderLayout.CENTER);

		
		//条件查找panel
		final JPanel conditionPn = new JPanel();
		conditionPn.setLayout(new GridLayout(7,1));
		
		JLabel conditionAgeLb = new JLabel("年龄：");
//		JTextField conditionAgeText = new JTextField();
		JComboBox conditionAgeCombo = new JComboBox(new String[]{"年龄不限","16-22","23-33","31-40","40-80"});
		JLabel conditionSexLb = new JLabel("性别：");
		JComboBox conditionSexCombo = new JComboBox(new String[]{"中","男","女"});
		JButton conditionCityBn = new JButton("城市");
		JLabel conditionIsOnlineLb = new JLabel("是否在线");
		JComboBox conditionIsCombo = new JComboBox(new String[]{"所有","在线"});
		//添加监听器
		ComboBoxListener myComboListener = new ComboBoxListener();
		conditionAgeCombo.addItemListener(myComboListener);
		conditionSexCombo.addItemListener(myComboListener);
		conditionIsCombo.addItemListener(myComboListener);		
		
		conditionPn.add(conditionAgeLb);
		conditionPn.add(conditionAgeCombo);
		conditionPn.add(conditionSexLb);
		conditionPn.add(conditionSexCombo);
		conditionPn.add(conditionCityBn);
		conditionPn.add(conditionIsOnlineLb);
		conditionPn.add(conditionIsCombo);
		
		
		final JLabel label = new JLabel();
		label.setText("当前在线人数");
		search_pn.add(label,BorderLayout.SOUTH);

		final JPanel pn_bt = new JPanel();

		bt_search = new JButton();
		bt_search.setText("查找");
		pn_bt.add(bt_search);
		
		
		bt_previous = new JButton();
		bt_previous.setText("上一步");
		pn_bt.add(bt_previous);
		bt_previous.setVisible(false);
		bt_previous.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				bt_search.setVisible(true);
				bt_addFriend.setVisible(false);
				bt_previous.setVisible(false);
				searchFrame.getContentPane().remove(result_pn);
				searchFrame.getContentPane().add(search_pn,BorderLayout.CENTER);
				searchFrame.repaint();
				searchFrame.setVisible(true);
			}});
		
		bt_addFriend = new JButton();
		bt_addFriend.setText("添加好友");
		bt_addFriend.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(result_pn_list.getSelectedValue()==null)
					return;
				StringTokenizer token = new StringTokenizer(result_pn_list.getSelectedValue().toString());
				writer.println("server");
				writer.println("addFriend");
				String id = token.nextToken();
				writer.println(id);
			}});
		pn_bt.add(bt_addFriend);
		bt_addFriend.setVisible(false);
		
		final JButton button_cancel = new JButton();
		button_cancel.setText("取消");
		pn_bt.add(button_cancel);
		button_cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				searchFrame.dispose();
			}});
		
		result_pn = new JPanel();
//		JLabel result_pn_lb = new JLabel("     以下是QQ为您查找的用户");
		JLabel result_pn_lb = new JLabel("     以下是QQ为您查找的用户",new ImageFactory().make("/image/search.gif"),2);
		result_pn_lb.setPreferredSize(new Dimension(150,50));
		result_pn_list = new JList(mainFm.readerThread.v);
		result_pn_list.setBorder(new LineBorder(Color.black,1));
		result_pn_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		result_pn_list.addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e) {
				System.out.println(result_pn_list.getSelectedValue());
				
			}});
		
		
		JPanel result_pn_pn = new JPanel();
		result_pn.setLayout(new BorderLayout());
		result_pn.add(result_pn_lb,BorderLayout.NORTH);
		result_pn.add(result_pn_list,BorderLayout.CENTER);
		result_pn.add(result_pn_pn,BorderLayout.SOUTH);
		
		JLabel lb_page = new JLabel("第几页");
		JLabel lb_previous = new JLabel("上页");
		JLabel lb_next = new JLabel("下页");
		result_pn_pn.add(lb_page);
		result_pn_pn.add(lb_previous);
		result_pn_pn.add(lb_next);
		
		searchFrame.getContentPane().setLayout(new BorderLayout());
		searchFrame.getContentPane().add(search_pn,BorderLayout.CENTER);
		searchFrame.getContentPane().add(pn_bt,BorderLayout.SOUTH);
		searchFrame.setVisible(true);
		
		bt_search.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				mainFm.readerThread.v.clear();
				if(bFind){
					if(exactSearch_text_Id.getText().trim().length()>0){
						try {
							int intId = Integer.parseInt(exactSearch_text_Id.getText().trim());
							writer.println("server");
							writer.println("searchFriend");
							writer.println("ID");
							writer.println(intId);
						} catch (NumberFormatException e1) {
							JOptionPane.showMessageDialog(searchFrame, "用户账号中有非法字符");
							return;
						}
						
					}else if(exactSearch_text_Name.getText().trim().length()!=0){
						writer.println("server");
						writer.println("searchFriend");
						writer.println("name");
						writer.println(exactSearch_text_Name.getText().trim());
					}else{
						JOptionPane.showMessageDialog(searchFrame, "请输入要查找的账号或者昵称");
						return;
					}
				}else{//此为条件查找，向服务器发送条件查找的信息
					String msg = age+","+sex+","+city+","+isOnline;
					System.out.println(msg);
					writer.println("server");
					writer.println("searchFriend");
					writer.println("con");
					writer.println(msg);
				}
				
			}});
		
		//添加事件监听器
		searchStyle_bt_exact.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				bFind = true;
				search_pn.remove(conditionPn);
				search_pn.add(exactSearch_pn,BorderLayout.CENTER);
				search_pn.repaint();
				searchFrame.setVisible(true);
				
			}});
		searchStyle_bt_condition.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				bFind = false;
				//移除精确查找panel，添加条件查找panel
				search_pn.remove(exactSearch_pn);
				search_pn.add(conditionPn,BorderLayout.CENTER);
				searchFrame.repaint();
				searchFrame.setVisible(true);
			}});
		
	}
	public void searchReturn(){
		bt_search.setVisible(false);
		bt_addFriend.setVisible(true);
		bt_previous.setVisible(true);
		searchFrame.getContentPane().remove(search_pn);
		searchFrame.getContentPane().add(result_pn,BorderLayout.CENTER);
		searchFrame.repaint();
		searchFrame.setVisible(true);
	}
	class ComboBoxListener implements ItemListener{
		public void itemStateChanged(ItemEvent e) {
			String name = e.getItem().toString();
			if(name.length()==1)
				sex = name;
			else if(name.length()==2)
				isOnline = name;
			else
				age = name;
			
		}
		
	}
}
